﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Download - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The Download function downloads a file from the Internet." />
<meta name="ahk:equiv-v1" content="commands/URLDownloadToFile.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Download</h1>

<p>Downloads a file from the Internet.</p>

<pre class="Syntax"><span class="func">Download</span> URL, Filename </pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>URL</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>URL of the file to download. 例如, http://someorg.org might retrieve the welcome page for that organization.</p>
  </dd>

  <dt>Filename</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>Specify the name of the file to be created locally, which is assumed to be in <a href="../Variables.htm#WorkingDir">A_WorkingDir</a> if an absolute path isn't specified. Any existing file will be <strong>overwritten</strong> by the new file.</p>
  </dd>

</dl>

<h2>ErrorLevel</h2>
<p>如果遇到问题, <a href="../misc/ErrorLevel.htm">ErrorLevel</a> 设置为 1, 否则设置为 0.</p>
<h2 id="Remarks">备注</h2>
<p>This function downloads to a file. To download to a variable instead, 请参阅 the example below. For alternative methods, 请参阅 the following forum topic: <a href="https://www.autohotkey.com/forum/topic10466.html">www.autohotkey.com/forum/topic10466.html</a></p>
<p>The download might appear to succeed even when the remote file doesn't exist. This is because many web servers send an error page instead of the missing file. This error page is what will be saved in place of <em>Filename</em>.</p>
<p>Firewalls or the presence of multiple network adapters may cause this function to fail. Also, some websites may block such downloads.</p>
<p><strong>Caching</strong>: By default, the URL is retrieved directly from the remote server (that is, never from Internet Explorer's cache). To permit caching, precede the URL with *0 followed by a space; 例如: <em>*0 http://someorg.org</em>. The zero following the asterisk may be replaced by any valid dwFlags number; for details, search <a href="http://www.microsoft.com">www.microsoft.com</a> for  InternetOpenUrl.</p>
<p><strong>Proxies</strong>: If a proxy server has been configured in Microsoft Internet Explorer's settings, it will be used.</p>
<p><strong>FTP and Gopher</strong> URLS are also supported. 例如:</p>
<pre>Download "ftp://example.com/home/My File.zip", "C:\My Folder\My File.zip"  <em>; Log in anonymously.</em>
Download "ftp://user:pass@example.com:21/home/My File.zip", "C:\My Folder\My File.zip"  <em>; Log in as a specific user.</em>
Download "ftp://user:pass@example.com/My Directory", "C:\Dir Listing.html"  <em>; Gets a directory listing in HTML format.</em></pre>

<h2 id="Related">相关</h2>
<p><a href="FileRead.htm">FileRead</a>, <a href="FileCopy.htm">FileCopy</a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>Download "http://autohotkey.com/download/2.0/version.txt", "C:\AutoHotkey Latest Version.txt"
Download "http://someorg.org/archive.zip", "C:\SomeOrg's Archive.zip"</pre>
</div>

<div class="ex" id="ExWHR">
<p><a href="#ExWHR">#2</a>: Download text to a variable:</p>
<pre>whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "http://autohotkey.com/download/2.0/version.txt")
whr.Send()
<em>; Using 'true' above and the call below allows the script to remain responsive.</em>
whr.WaitForResponse()
version := whr.ResponseText
MsgBox version
</pre>
</div>

<div class="ex" id="ExXHR">
<p><a href="#ExXHR">#3</a>: Make an asynchronous HTTP request.</p>
<pre>req := ComObjCreate("Msxml2.XMLHTTP")
<em>; Open a request with async enabled.</em>
req.open("GET", "https://www.autohotkey.com/download/2.0/version.txt", true)
<em>; Set our callback function.</em>
req.onreadystatechange := Func("Ready")
<em>; Send the request.  Ready() will be called when it's complete.</em>
req.send()
<em>/*
; If you're going to wait, there's no need for onreadystatechange.
; Setting async=true and waiting like this allows the script to remain
; responsive while the download is taking place, whereas async=false
; will make the script unresponsive.
while req.readyState != 4
    sleep 100
*/</em>
#Persistent

Ready() {
    global req
    if (req.readyState != 4)  <em>; Not done yet.</em>
        return
    if (req.status == 200) <em>; OK.</em>
        MsgBox "Latest AutoHotkey version: " req.responseText
    else
        MsgBox "Status " req.status,, 16
    ExitApp
}</pre>
</div>

</body>
</html>